package org.corey.share.auth;

import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

public class DefaultAuthFileParsor implements AuthFileParsor {
	public static final String RESOURCE_TOKEN = ",";

	@Override
	public RoleAccessStrategory parse(String filePath) {
		Properties prop = new Properties();
		try {
			prop.load(this.getClass().getClassLoader().getResourceAsStream(filePath));
		} catch (Exception e) {
			throw new RuntimeException(
					"the permission file is not found in classpath");
		}
		String resources = prop.getProperty("admin");
		String[] resourceList = resources
				.split(DefaultAuthFileParsor.RESOURCE_TOKEN);
		Set<String> plainResources = new HashSet<String>();
		Set<String> expressResources = new HashSet<String>();
		if (resourceList != null) {
			for (int index = 0; index < resourceList.length; index++) {
				String resource = resourceList[index].trim();
				if (resource.endsWith("*")) {
					expressResources.add(resource);
				} else {
					plainResources.add(resource);
				}
			}
		}
		return new DefaultRoleAccessStrategory("admin", plainResources,
				expressResources);
	}
}
